package com.example.controller;

import com.example.entity.po.Teacher;
import com.example.utils.ResultUtils;
import com.example.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
@RestController
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    @GetMapping("/teacher")
    @ResponseBody
    public ResultUtils teacherList(){
        List<Teacher> list=teacherService.queryTeacher();
        if (list.size()==0){
            return ResultUtils.Failure("获取教师信息失败", null);
        }else {
            return ResultUtils.Successful("获取教师信息成功",list);
        }
    }

    @PostMapping("/deleteTeacher")
    @ResponseBody
    public ResultUtils deleteStudent(String teacherId){
        int res= teacherService.deleteTeacher(teacherId);
        if (res==0){
            return ResultUtils.Failure("删除教师信息失败",null);
        }else {
            return ResultUtils.Successful("删除教师信息成功",null);
        }
    }

    @PostMapping("/updateTeacher")
    @ResponseBody
    public ResultUtils updateTeacher(@RequestBody Teacher teacher){
        int res= teacherService.updateTeacher(teacher);
        if (res==0){
            return ResultUtils.Failure("修改失败",null);
        }else {
            return ResultUtils.Successful("修改成功",null);
        }
    }

    @PostMapping("/addTeacher")
    @ResponseBody
    public ResultUtils addTeacher(@RequestBody Teacher teacher){
        int res= teacherService.addTeacher(teacher);
        if (res==0){
            return ResultUtils.Failure("添加失败，输入教师编号错误",null);
        }else {
            return ResultUtils.Successful("添加成功",null);
        }
    }

    @PostMapping("/fuzzyTeacher")
    public ResultUtils fuzzyTeacher(String teacherName){
        List<Teacher> teacher=teacherService.fuzzyTeacher(teacherName);
        if (teacher.size()==0){
            return ResultUtils.Failure("搜索失败或无该教师",null);
        }else {
            return ResultUtils.Successful("搜索教师成功",teacher);
        }
    }
}
